package com.wwe.kotlinstudy

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val textView = findViewById<TextView>(R.id.text)

        val str = """
              for (c in "foo")
                  print(c)
            """

        textView.text = str
    }

    fun sum(a: Int, b: Int): Int {
        return a + b
    }

    fun sum1(a: Int, b: Int) = a + b

    //函数返回无意义的值Unit
    fun printSum(a: Int, b: Int): Unit {
        println("sum of $a and $b is ${a + b}")
    }

    //返回值允许为空，所以加一个?
    fun parseInt(str: String): Int? {
        //......
        return Int.MAX_VALUE
    }

    fun printProduct(arg1: String, arg2: String) {
        val x = parseInt(arg1)
        val y = parseInt(arg2)
        //println(x*y) 直接使用x*y会导致编译错误，必须进行空检测

        if (x == null) {
            println("Wrong number format in arg1: '$arg1'")
            return
        }

        if (y == null) {
            println("Wrong number format in arg2 : '$arg2'")
            return
        }

        //空检测完成之后，x,y会自动转换为非空的值
        println(x * y)
    }















}
